package com.suse.lhw.hms.authorizaiontserver.service.Impl;


import com.suse.lhw.hms.authorizaiontserver.dao.UPermissionMapper;
import com.suse.lhw.hms.authorizaiontserver.dao.URoleMapper;
import com.suse.lhw.hms.authorizaiontserver.dao.URolePermissionMapper;
import com.suse.lhw.hms.authorizaiontserver.dao.URoleUserMapper;
import com.suse.lhw.hms.authorizaiontserver.domin.UPermission;
import com.suse.lhw.hms.authorizaiontserver.domin.URole;
import com.suse.lhw.hms.authorizaiontserver.domin.URolePermission;
import com.suse.lhw.hms.authorizaiontserver.domin.URoleUser;
import com.suse.lhw.hms.authorizaiontserver.service.URoleService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.security.service.Impl
 * @ClassName: URoleServiceImpl
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2021/1/31 19:24
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/1/31 19:24
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
public class URoleServiceImpl implements URoleService {

    @Resource
    private URoleMapper uRoleMapper;
    @Resource
    private URoleUserMapper uRoleToUserMapper;
    @Resource
    private URolePermissionMapper uRolePermissionMapper;
    @Resource
    private UPermissionMapper uPermissionMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return uRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(URole record) {
        return uRoleMapper.insert(record);
    }

    @Override
    public int insertSelective(URole record) {
        return uRoleMapper.insertSelective(record);
    }

    @Override
    public URole selectByPrimaryKey(Integer id) {
        return uRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(URole record) {
        return uRoleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(URole record) {
        return uRoleMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<GrantedAuthority> selectRoleListByUserId(String userId) {
        //通过userid在role——user里面找role，
        List<URoleUser> uToRoleList = uRoleToUserMapper.selectAllByUserid(userId);
        String roleNameString = "";
        for (URoleUser uRoleUser : uToRoleList) {
            URole uRole = uRoleMapper.selectByPrimaryKey(uRoleUser.getRoleId());
            //根据角色找权限,整成list之后再用逗号链接通过commaSeparatedStringToAuthorityList整成grantedauthority
            List<URolePermission> uRolePermissionList = uRolePermissionMapper.selectAllByRoleId(uRole.getId());
            for (URolePermission uRolePermission : uRolePermissionList) {
                UPermission uPermission = uPermissionMapper.selectByPrimaryKey(uRolePermission.getPermissionId());
                if (uPermission != null || uPermission.getName() != null) {
                    roleNameString += uPermission.getName() + ",";
                    System.out.println("权限有" + uPermission.getName());
                }
            }
        }

        return AuthorityUtils.commaSeparatedStringToAuthorityList(roleNameString);
    }

}
